package com.globalwave.util
{
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	
	public class TreeUtil
    {
        public function TreeUtil()
        {
            super();
        }
        
        public static function buildTree(
                listDataProvider : Object, 
                rootDisplayName: String ,
                labelField: String ,
                idFieldName: String ,
                proIdFieldName: String ,
                stateFieldName : String 
        ) : XML {
        
            var list : XML = new XML(listDataProvider) ;
            //list = new XML(list.elements("datas")) ;

            var tree : XML = null ;
            
            if (rootDisplayName != null) {
                tree = <root {idFieldName}=""></root>;    
                tree[labelField] = rootDisplayName ;
            }
            
            var items : XML = <root></root> ;
            
            for each (var elem :XML in list.children()) {
                
                var item : XML = null ;
                
                
                if (tree == null) {
                    item = <root></root> ;
                } else {
                    item = <item></item> ;
                }
                
                if (elem[stateFieldName].toString() != "") {
                    item.@state = "checked"
                } else {
                    item.@state = "unchecked"
                }
                
                item.@isBranch = "false" ;
                
                
                for each (var attrElem : XML in elem.children()) {
                    item.@[attrElem.name().toString()] = attrElem.valueOf().toString() ;
                }
                
                if (tree == null) {
                    tree = item.copy() ;
                } else {
                    items.appendChild(item) ;
                }
            }
            
            
            for (var i : int = items.children().length() - 1 ; i >=0 ; i -- ) {
                var pro : String = items.item[i].@[proIdFieldName] ;
                
                if (pro == "" || pro == tree.@[idFieldName]) {
                    tree.prependChild(items.item[i]) ;        
                    tree.@isBranch = "true" ;
                } else {
                    items.item.(@[idFieldName] == pro ).@isBranch = "true" ;
                    items.item.(@[idFieldName] == pro ).prependChild(items.item[i]) ;
                }
            }
                        
            return tree ;    
                            
        }
        
        
        
        public static function toNode(
                treeNode : Object,
                stateFieldName : String 
        ) : XML {
        
            var elem : XML = new XML(treeNode) ;
                
            var item : XML = <item></item> ;
            
            if (elem != null) {
                
                if (elem[stateFieldName].toString() != "") {
                    item.@state = "checked"
                } else {
                    item.@state = "unchecked"
                }
                
                item.@isBranch = "false" ;
                
                
                for each (var attrElem : XML in elem.children()) {
                    item.@[attrElem.name().toString()] = attrElem.valueOf().toString() ;
                }
            
            }
            return item ;
                            
        }
        
        public static function addNode(
                tree : XML, 
                treeNode : Object,
                idFieldName: String ,
                proIdFieldName: String ,
                stateFieldName : String 
        ) : void {
        
            var proIdValue : String = null ;
            var elem : XML = new XML(treeNode) ;
                
            var item : XML = <item></item> ;
            
            if (elem != null) {
                
                if (elem[stateFieldName].toString() != "") {
                    item.@state = "checked"
                } else {
                    item.@state = "unchecked"
                }
                
                item.@isBranch = "false" ;
                
                
                for each (var attrElem : XML in elem.children()) {
                    if ( proIdFieldName == attrElem.name().toString()) {
                        proIdValue = attrElem.valueOf().toString() ;
                    }
                    item.@[attrElem.name().toString()] = attrElem.valueOf().toString() ;
                }
            
            }
            Alert.show(tree.descendants().toXMLString()) ;
            tree.descendants().(@idFieldName == proIdValue).appendChild(item) ;
            //tree.elements("//item[@id=" + proIdValue + "]").appendChild(item) ;
                            
        }
        
        
        public static function getSelectedIds(dataProvider : XML, idFieldName : String) : ArrayCollection {
            var ids : ArrayCollection = new ArrayCollection() ;
             getNodeSelectedIds(dataProvider, idFieldName, ids) ;
             
             return ids ;
        }
        
        private static function getNodeSelectedIds(xml : XML , idFieldName : String, ids : ArrayCollection) :void {
            for (var i : int = 0 ; xml != null && i < xml.children().length() ; i ++) {
                if (xml.children()[i].@state.toString() == "checked") {
                    ids.addItem(xml.children()[i].@[idFieldName].toString()) ;
                }
                getNodeSelectedIds(xml.children()[i], idFieldName, ids) ;
            }
        }
        
        public static function resetState(dataProvider : XML, stateFieldName : String) : void {
            resetNodeState(dataProvider, stateFieldName) ;
        }
        
        private static function resetNodeState(xml : XML , stateFieldName : String) :void {
            for (var i : int = 0 ; xml != null && i < xml.children().length() ; i ++) {

                if (xml.children()[i].@[stateFieldName].toString() != "") {
                    xml.children()[i].@state = "checked"
                } else {
                    xml.children()[i].@state = "unchecked"
                }
                
                resetNodeState(xml.children()[i], stateFieldName) ;
            }
        }
        
    }        

}